/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.util;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.servlet.ServletContext;

import static java.util.logging.Level.INFO;

/**
 * Often a file, resource can be located in several places because
 * Redora allows custom versions of files. So, when looking for a
 * file, first the custom version should be considered. If not
 * found the a local source version of Redora should be checked,
 * this is necessary when you develop Redora itself,
 * it should get the latest version.
 * If those not exist, it will look for the file in the class path.<br>
 * In all cases an InputStream to the needed file is returned.
 *
 * @author Nanjing RedOrange (www.red-orange.cn)
 */
public class ResourceFileHandler {
    private static final transient Logger l = Logger.getLogger("redora.util.ResourceFileHandler");

    private final static String LOCAL_UPGRADE_PATH = "src"
                + File.separator + "main" + File.separator + "resources"
                + File.separator + "upgrade" + File.separator;
    private final static String RESOURCE_UPGRADE_PATH = "/upgrade/";

    private final static String CUSTOM_POLICY_PATH = "src"
                + File.separator + "main" + File.separator + "resources"
                + File.separator + "policies" + File.separator;
    private final static String CUSTOM_POLICY_RESOURCE = "/policies/";
    private static final String RESOURCE_POLICY_PATH = "/";

    /**
     * Searches source by source until it finds your file.
     *
     * @param sources Files and Strings. String will be consider
     * a locator on the class path.
     * @return Null or the requested inputstream
     */
    @Nullable
    public static InputStream find(@NotNull Object[] sources, @Nullable ServletContext context)
              throws FileNotFoundException {
        for (Object source : sources) {
            if (source instanceof File) {
                if (((File)source).exists()) {
                    return new FileInputStream((File)source);
                }
                l.log(INFO, "Can''t find file {0}", ((File) source).getAbsolutePath());
            } else { //source is located in the classpath or war context
                if (context != null) {
                    InputStream in = context.getResourceAsStream("/WEB-INF" + (String)source);
                    if (in != null) {
                        return in;
                    }
                    l.log(INFO, "Can''t find web resource {0}", "/WEB-INF" + source);
                }
                InputStream in = ResourceFileHandler
                .class.getResourceAsStream((String)source);
                if (in != null) {
                    return in;
                }
                l.log(INFO, "Can''t find web class resource {0}", source);
            }
        }
        return null;
    }

    /**
     * Finds the file. First it will try the local src/main/resources/upgrade
     * folder. Then it will try the class path in /upgrade
     *
     * @param fileName (Mandatory) filename without path including the .sql suffix.
     * @param context (Optional) when in a servlet container, you need this when you want to find the file in the classpath
     * @return Null or the InputStream.
     */
    @Nullable
    public static InputStream findUpgradeFile(@NotNull String fileName,
            @Nullable ServletContext context) throws FileNotFoundException {
        Object[] sources = {new File(LOCAL_UPGRADE_PATH + fileName),
                RESOURCE_UPGRADE_PATH + fileName};
        return find(sources, context);
    }

    /**
     * Locates AntiSamy policy files. The user can provide a
     * custom policy file in his resources/policies directory.
     * And finally the class path in policies.
     * @param policy (Mandatory) The policy file name including the .xml suffix.
     * @return Null or the InputStream
     */
    @Nullable
    public static InputStream findPolicy(@NotNull String policy)
                 throws FileNotFoundException {
        Object[] sources = {
                new File(CUSTOM_POLICY_PATH + policy)
                , RESOURCE_POLICY_PATH + policy
                , CUSTOM_POLICY_RESOURCE + policy};
        return find(sources, null);
    }
}
